package com.kqzz.common.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kqzz.common.config.mapper.MyMapper;
import com.kqzz.common.dynamicDB.annotation.DataSource;
import com.kqzz.common.service.IService;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

/**
 * @Description 实现Service基类接口中通用方法的抽象类
 * @ClassName BaseService.java
 * @Package com.kanq.zz.business.service.impl 
 * @author lipeng
 * @date 2018年8月14日 上午11:18:51
 * 
 * @update_Desc
 * @update_Author
 * @update_Date
 *
 *  ██╗     ██╗    ██████╗ ███████╗███╗   ██╗ ██████╗ 
 *	██║     ██║    ██╔══██╗██╔════╝████╗  ██║██╔════╝ 
 *	██║     ██║    ██████╔╝█████╗  ██╔██╗ ██║██║  ███╗
 *	██║     ██║    ██╔═══╝ ██╔══╝  ██║╚██╗██║██║   ██║
 *	███████╗██║    ██║     ███████╗██║ ╚████║╚██████╔╝
 *	╚══════╝╚═╝    ╚═╝     ╚══════╝╚═╝  ╚═══╝ ╚═════╝
 */
@Slf4j
public abstract class BaseService<T> implements IService<T> {

	@Autowired
	protected MyMapper<T> mapper;

	public MyMapper<T> getMapper() {
		return mapper;
	}
	
	@DataSource("slave1")
	@Override
	public List<T> selectAll() {
		log.info("调用[selectAll]函数");
		return mapper.selectAll();
	}

	@DataSource("slave1")
	@Override
	public T selectByKey(Object key) {
		log.info("调用[selectByKey]函数");
		return mapper.selectByPrimaryKey(key);
	}

	@DataSource("slave1")
	@Override
	@Transactional
	public int save(T entity) {
		log.info("调用[save]函数");
		return mapper.insert(entity);
	}
	
	@DataSource("slave1")
	@Override
	@Transactional
	public int saveNotNull(T entity) {
		log.info("调用[saveNotNull]函数");
		return mapper.insertSelective(entity);
	}

	@DataSource("slave1")
	@Override
	@Transactional
	public int delete(Object key) {
		log.info("调用[delete]函数");
		return mapper.deleteByPrimaryKey(key);
	}
	
	@DataSource("slave1")
	@Override
	@Transactional
	public int batchDelete(List<String> list, String property, Class<T> clazz) {
		log.info("调用[batchDelete]函数");
		if(list!=null){
			Example example = new Example(clazz);
			example.createCriteria().andIn(property, list);
			return this.mapper.deleteByExample(example);
		}else{
			return this.mapper.deleteByExample(null);
		}
	}
	
	@DataSource("slave1")
	@Override
	@Transactional
	public int deleteLogic(Object key) {
		log.info("调用[deleteLogic]函数");
		return mapper.deleteLogicByIds(key);
	}
	
	@DataSource("slave1")
	@Override
	@Transactional
	public int batchDeleteLogic(List<String> list, String property, Class<T> clazz) {
		log.info("调用[batchDeleteLogic]函数");
		if(list!=null){
			Example example = new Example(clazz);
			example.createCriteria().andIn(property, list);
			return this.mapper.deleteLogicByExample(example);
		}else{
			return this.mapper.deleteLogicByExample(null);
		}
	}

	@DataSource("slave1")
	@Override
	@Transactional
	public int updateAll(T entity) {
		log.info("调用[updateAll]函数");
		return mapper.updateByPrimaryKey(entity);
	}

	@DataSource("slave1")
	@Override
	@Transactional
	public int updateNotNull(T entity) {
		log.info("调用[saveNotNull]函数");
		return mapper.updateByPrimaryKeySelective(entity);
	}

	@DataSource("slave1")
	@Override
	public List<T> selectByExample(Object example) {
		log.info("调用[selectByExample]函数");
		return mapper.selectByExample(example);
	}
	
	@DataSource("slave1")
	@Override
	public PageInfo<T> selectAll(Object example, int currentPage, int pageSize) {
		// TODO Auto-generated method stub
		log.info("调用[selectAll]分页函数");
		if(currentPage==0){
			currentPage=1;
		}
		PageHelper.startPage(currentPage,pageSize);
		List<T> list= mapper.selectByExample(example);
		
		PageInfo<T> pageInfo= new PageInfo<T>(list);
		return pageInfo;
	}
}
